﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QuanLyBanHang.Lib;

namespace QuanLyBanHang.GUI
{
    public partial class frmNhomSanPham : Form
    {
        #region "Variables"
        private DataTable _dtbLoaiSP = KetNoi.GetTable("select * from tblLoaiSanPham");
        public int i = 0;
        #endregion
        public frmNhomSanPham()
        {
            InitializeComponent();
        }

        private void frmNhomSanPham_Load(object sender, EventArgs e)
        {
            Loaddtg();
            Lock();
        }

        public void Unlock()
        {
            txtMaLoaiSP.Enabled = true;
            txtTenLoaiSP.Enabled = true;
            txtTinhTrang.Enabled = true;
            txtGhiChu.Enabled = true;
            txtMaLoaiSP.Text = "";
            txtTenLoaiSP.Text = "";
            txtGhiChu.Text = "";
            txtTinhTrang.Text = "";
        }
        public void Lock()
        {
            btnXoa.Enabled = false;
            btnSua.Enabled = false;
            btnLuu.Enabled = false;
            txtMaLoaiSP.Enabled = false;
            txtTenLoaiSP.Enabled = false;
            txtTinhTrang.Enabled = false;
            txtGhiChu.Enabled = false;
            txtMaLoaiSP.Text = "";
            txtTenLoaiSP.Text = "";
            txtGhiChu.Text = "";
            txtTinhTrang.Text = "";
        }
        

        private void btnNew_Click(object sender, EventArgs e)
        {
            Unlock();
            btnLuu.Enabled = true;
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            if (txtMaLoaiSP.Enabled == true)
            {

                DataRow newr = _dtbLoaiSP.NewRow();
                newr["MaLoaiSP"] = txtMaLoaiSP.Text;
                newr["TenLoaiSP"] = txtTenLoaiSP.Text;
                newr["GhiChu"] = txtGhiChu.Text;
                newr["TrangThai"] = txtTinhTrang.Text;
                i = KetNoi.ExcuteSQL("insert into tblLoaiSanPham values('" + newr["MaLoaiSP"] + "',N'" + newr["TenLoaiSP"] + "',N'" + newr["GhiChu"] + "',N'" + newr["TrangThai"] + "')");
                if (i > 0)
                {
                    MessageBox.Show("Luu thanh cong");
                }
                else
                    MessageBox.Show("Thất bại");
                Loaddtg();
                Lock();
            }
            else
            {
                DataRow newr = _dtbLoaiSP.NewRow();
                newr["MaLoaiSP"] = txtMaLoaiSP.Text;
                newr["TenLoaiSP"] = txtTenLoaiSP.Text;
                newr["GhiChu"] = txtGhiChu.Text;
                newr["TrangThai"] = txtTinhTrang.Text;
                i = KetNoi.ExcuteSQL("update tblLoaiSanPham set TenLoaiSP = N'" + newr["TenLoaiSP"] + "',GhiChu = N'" + newr["GhiChu"] + "',TrangThai = N'" + newr["TrangThai"] + "' where MaLoaiSP = '" + newr["MaLoaiSP"] + "'");
                if (i > 0)
                {
                    MessageBox.Show("Luu thanh cong");
                }
                else
                    MessageBox.Show("Thất bại");
                Loaddtg();
            }
        }

        bool checktrong(Control ctlcheck)// textbox,combobox,..
        {
            string type = ctlcheck.GetType().Name;
          
            foreach (Control c in tableLayoutPanel1.Controls)
            {
                switch (type.ToLower())
                {
                    case "Table..":
                        checktrong(c);
                        break;
                }
            }
            if (ctlcheck != null)
                if (ctlcheck.Text.Trim() == "")
                    return false;
                else return true;
            return false;
        }

        public void Loaddtg()
        {
            dtgNhomSP.DataSource = KetNoi.GetTable("select * from tblLoaiSanPham");
            dtgNhomSP.AllowUserToAddRows = false;
            dtgNhomSP.AllowUserToDeleteRows = false;
            dtgNhomSP.Columns["MaLoaiSP"].HeaderText = "Mã loại sản phẩm";
            dtgNhomSP.Columns["TenLoaiSP"].HeaderText = "Tên loại sản phẩm";
            dtgNhomSP.Columns["GhiChu"].HeaderText = "Ghi chú";
            dtgNhomSP.Columns["TrangThai"].HeaderText = "Trạng thái";
        }
        private void dtgNhomSP_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            txtMaLoaiSP.Enabled = false;
            btnSua.Enabled = true;
            if (e.RowIndex >= 0)
            {
                txtMaLoaiSP.Text = _dtbLoaiSP.Rows[e.RowIndex]["MaLoaiSP"].ToString();
                txtTenLoaiSP.Text = _dtbLoaiSP.Rows[e.RowIndex]["TenLoaiSP"].ToString();
                txtGhiChu.Text = _dtbLoaiSP.Rows[e.RowIndex]["GhiChu"].ToString();
                txtTinhTrang.Text = _dtbLoaiSP.Rows[e.RowIndex]["TrangThai"].ToString();
            }
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            if ((txtMaLoaiSP.Enabled == true) &&(txtMaLoaiSP.Text == null))
            {
                MessageBox.Show("Chua chon nhom san pham");
            }
            else
            {
                i = KetNoi.ExcuteSQL("delete  from tblLoaiSanPham where MaLoaiSP = '" + txtMaLoaiSP.Text + "'");
                if (i > 0)
                {
                    MessageBox.Show("Xóa thành công");
                    Loaddtg();
                }
                else
                {
                    MessageBox.Show("Xóa thất bại");
                }
            }
        }

        private void dtgNhomSP_MouseLeave(object sender, EventArgs e)
        {
            btnSua.Enabled = false;
        }

        private void txtTimKiem_TextChanged(object sender, EventArgs e)
        {
            DataTable dtbNhomHang = KetNoi.GetTable("select * from tblLoaiSanPham where MaLoaiSP like N'%"+txtTimKiem.Text+"%' or TenLoaiSP like N'%"+txtTimKiem.Text+"%' ");
            dtgNhomSP.DataSource = dtbNhomHang;
            dtgNhomSP.AllowUserToAddRows = false;
            dtgNhomSP.AllowUserToDeleteRows = false;
            dtgNhomSP.Columns["MaLoaiSP"].HeaderText = "Mã loại sản phẩm";
            dtgNhomSP.Columns["TenLoaiSP"].HeaderText = "Tên loại sản phẩm";
            dtgNhomSP.Columns["GhiChu"].HeaderText = "Ghi chú";
            dtgNhomSP.Columns["TrangThai"].HeaderText = "Trạng thái";
        }
    }
}
